﻿using ProjectCore.Application.AppUser.Dto;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using ProjectCore.Domain.Model.Entity;
using System.Linq;
using ProjectCore.Domain.DomainService;
using System.Threading.Tasks;
using ProjectCore.Common;

namespace ProjectCore.Application
{
    public class AppProduct : IAppProduct
    {
        private readonly IBaseRepository<PproductType> baseRepository;
        private readonly MyContext myContext;
        private readonly IProductDomainService product1;
        public AppProduct(IBaseRepository<PproductType> _baseRepository, MyContext _myContext, IProductDomainService _productDomainService)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
            product1 = _productDomainService;
        }


        //商品添加
        public async Task<int> AddProduct(Product product)
        {
            return await product1.AddProduct(product);
        }


        //反填
        public async Task<Product> GetProduct(int Id)
        {
            return await product1.GetProduct(Id);
        }


        //商品显示
        public async Task<HeaderResult<List<PproductType>>> GetPproductTypes(int Tid, int Sta, string Na, int pageIndex, int pageSize)
        {
            try
            {
                var l = from a in myContext.Products
                        join b in myContext.ProductTypes
                        on a.TypeID equals b.ProTypeID into tp
                        from tt in tp.DefaultIfEmpty()
                        join c in myContext.PicUrls
                        on a.Photo equals c.ID into pi
                        from pp in pi.DefaultIfEmpty()
                        where (Tid == 0 || a.TypeID.Equals(Tid)) && (Sta == 0 || a.State.Equals(Sta)) && (string.IsNullOrEmpty(Na) || a.ProName.Contains(Na))
                        select new PproductType
                        {
                            ProID = a.ProID,
                            Photo = pp.ID == 0 ? "" : pp.Photo,
                            ProName = a.ProName,
                            ProTypeName = tt.ProTypeName,
                            Sales = a.Sales,
                            Sort = a.Sort,
                            State = a.State,
                            CreateTime = a.CreateTime
                        };
                int d = l.ToList().Count();
                l = l.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                HeaderResult<List<PproductType>> result = new HeaderResult<List<PproductType>>()
                {
                    IsSucceed = true,
                    Result = await l.ToListAsync(),
                    Total = d,
                };
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }


        //商品逻辑删除
        public async Task<int> IsDel(int Id)
        {
            return await product1.IsDel(Id);
        }


        //其他设置添加
        public async Task<int> SettingAdd(Setting setting)
        {
            return await product1.SettingAdd(setting);
        }


        //其他设置反填
        public async Task<Setting> GetSetting(int Id)
        {
            return await product1.GetSetting(Id);
        }


        //其他设置修改
        public async Task<int> SettingUpt(Setting setting)
        {
            return await product1.SettingUpt(setting);
        }


        //汽车Sku添加
        public async Task<int> SkuCarAdd(SkuCar car)
        {
            return await product1.SkuCarAdd(car);
        }


        //汽车Sku反填
        public async Task<SkuCar> GetCar(int Id)
        {
            return await product1.GetCar(Id);
        }


        //汽车Sku修改
        public async Task<int> SkuCarUpt(SkuCar car)
        {
            return await product1.SkuCarUpt(car);
        }


        //手机Sku添加
        public async Task<int> SkuPhoneAdd(SkuPhone phone)
        {
            return await product1.SkuPhoneAdd(phone);
        }


        //手机Sku反填
        public async Task<SkuPhone> GetPhone(int Id)
        {
            return await product1.GetPhone(Id);
        }


        //手机Sku修改
        public async Task<int> SkuPhoneUpt(SkuPhone phone)
        {
            return await product1.SkuPhoneUpt(phone);
        }


        //修改
        public async Task<int> UptProduct(Product product)
        {
            return await product1.UptProduct(product);
        }
    }
}
